/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package flightlifter;

import java.awt.Font;
import java.util.Vector;
import org.lwjgl.input.Controller;
import org.lwjgl.opengl.GL11;

/**
 *
 * @author Matt
 */
public class JoystickAxis extends WiringNode implements WiringOutput {

    private int axis = 0;
    private Vector<WiringConsumer> consumers = new Vector();
    private Controller controller;
    private float outVal = 0;
    private int consumerIndex = 0;  //this is a remp variable to hold a pointer to
    //sometimes wirelss controllers spit out -1 until you transmit a sig
    //this prevents the control box from doing anything silly until we
    //know we have a good feed from the joystick
    private boolean inited = false;
    private float lastVal;
    //the consumer until we can be sure the consumer
    //has been instantiated
    public int type;               //does this point to a node or a constraint as an output?
    Vector<ConnectionInfo> connections = new Vector();

    public void setConsumer(WiringConsumer consumer) {

        consumers.add(consumer);

    }

    public JoystickAxis(int axis, Controller controller, TrueTypeFont font,
            Vector<Selectable> selectSet,int x, int y) {
        this.axis = axis;
        this.controller = controller;
        this.font = font;
        locX = x;
        locY = y;
        this.selectSet = selectSet;
        lastVal = controller.getAxisValue(axis);
    }

    public void evaluate() {
        outVal = controller.getAxisValue(axis);
        if (lastVal != outVal && !inited) {
            inited = true;
        }
        lastVal = outVal;
        if (!inited) {
            outVal = 0.f;
        }

        for (WiringConsumer c : consumers) {


            //catch a delta to signal joystick input has commenced

            c.setValue(outVal);
        }
        finished = true;


    }

    public void lineToConsumer() {

        if (consumers != null) {
            PVector center;

            GL11.glColor3f(0, 0, 1);

            GL11.glPushMatrix();



            for (WiringConsumer c : consumers) {
                center = c.getScreenCenter();
                
                GL11.glBegin(GL11.GL_LINES);
                GL11.glVertex3f(locX + cWidth, locY + cHeight / 2, 0);
                //connect nodes at the edges. For objects on the planes, go right to the center
                if (c instanceof WiringNode) {
                    GL11.glVertex3f(center.x - cWidth / 2, center.y, 0);
                } else {
                    GL11.glVertex3f(center.x, center.y, 0);
                }

                GL11.glEnd();
            }
            GL11.glPopMatrix();
        }

    }
    public void draw() {
        super.draw();
        lineToConsumer();
        GL11.glColor4f(parent.TEXT[0], parent.TEXT[1], parent.TEXT[2], parent.TEXT[3]);
        //parent.font.drawString(locX + 5, locY, title, 1, 1);
        font.drawString(locX + 5, locY, "Axis " + (axis + 1) + ": " + outVal, 1f, 1f);

    }
    
   

    public void hookUp(Vector<Node> nodes,
            Vector<Node> constraints,
            Vector<WiringNode> wiring) {



        for (ConnectionInfo ci : connections) {
            System.out.println("adding connection" + ci.type + ":" + ci.index);
            if (ci.index != -1) {
                if (ci.type == 0) {
                    setConsumer((WiringConsumer) wiring.elementAt(ci.index));
                } else if (ci.type == 1) {
                    setConsumer((WiringConsumer) nodes.elementAt(ci.index));
                } else if (ci.type == 2) {
                    setConsumer((WiringConsumer) constraints.elementAt(ci.index));
                }

            }
        }

    }

    public JoystickAxis(String[] line, Controller controller, TrueTypeFont font, Vector <Selectable> selected) {
        this(Integer.parseInt(line[1]),
                controller,
                font,
                selected,
                Integer.parseInt(line[2]),
                Integer.parseInt(line[3]));

        for (int i = 4; i < line.length; i += 2) {
            connections.add(new ConnectionInfo(Integer.parseInt(line[i]),
                    Integer.parseInt(line[i + 1])));

        }
    }

    public String pickle(Vector<Node> nodes,
            Vector<Node> constraints,
            Vector<WiringNode> wiring) {

        String ret = "J,"
                + axis + ","
                + locX + ","
                + locY + ",";
        for (WiringConsumer wc : consumers) {
            if (wc instanceof WiringNode) {
                ret += 0 + "," + wiring.indexOf(wc) + ",";
            } else if (wc instanceof Coupling || wc instanceof ControllerConstraint) {
                ret += 2 + "," + constraints.indexOf(wc) + ",";
            }else {
                ret += 1 + "," + nodes.indexOf(wc) + ",";
            }

        }
        return ret;
    }
}


